package com.imachen.org.controller;

import  com.imachen.basic.utils.AjaxResult;
import com.imachen.basic.utils.PageList;
import com.imachen.org.domain.Department;
import com.imachen.org.query.DepartmentQuery;
import com.imachen.org.service.IDepartmentService;
import com.imachen.system.annotation.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * @description: 部门接口类
 * @author:
 * @date: 2022/8/14 0014 : 10:37
 * @version: ver1.0
 */
@Api(value = "部门接口类",description = "部门接口类")
@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;
    /**
     * @description: 查询所有
     * @author:
     * @date: 2022/8/14 0014 : 10:38
     * @param:
     * @return:
     */
    @ApiOperation(value = "查询所有部门信息")
    @GetMapping

    public List<Department> finAll(){
        return departmentService.findAll();
    }

    /**
     * @description: 查询单条
     * @author: AChen
     * @date: 2022/8/14 0014 : 10:38
     * @param:
     * @return:
     */
    @ApiOperation(value = "查询部门单条数据")
    @GetMapping("{id}")
    public Department findOne(@PathVariable("id")Long id){
        return departmentService.findOneById(id);
    }

    /**
     * @description: 删除数据
     * @author: AChen
     * @date: 2022/8/14 0014 : 10:41
     * @param:
     * @return:
     */
    @ApiOperation(value = "删除部门数据")
    @DeleteMapping("{id}")
    @PreAuthorize(name = "部门删除",sn = "department:delete")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            departmentService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }
    
    /**
     * @description: 添加或修改
     * @author: AChen
     * @date: 2022/8/14 0014 : 10:41
     * @param:
     * @return:
     */
    @ApiOperation(value = "添加或修改部门信息")
    @PutMapping
    @PreAuthorize(name = "部门保存",sn = "department:add")
    public AjaxResult add0rUpt(@RequestBody Department department){
        try {
            if (department.getId() == null){
                departmentService.add(department);
            }else {
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * @description: 分页
     * @author: AChen
     * @date: 2022/8/14 0014 : 11:06
     * @param:
     * @return:
     */
    @ApiOperation(value = "分页查询部门信息")
    @PostMapping
    @PreAuthorize(name = "部门列表",sn = "department:list")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryPage(departmentQuery);
    }

    /**
     * @description: 批量删除
     * @author: AChen
     * @date: 2022/8/15 0015 : 14:41
     * @param:
     * @return:
     */
    @ApiOperation(value = "批量删除")
    @PatchMapping
    @PreAuthorize(name = "部门批量删除",sn = "department:patchDelete")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }
    
    /**
     * @description: 无限极树
     * @author: AChen
     * @date: 2022/8/15 0015 : 19:34
     * @param: 
     * @return: 
     */
    @ApiOperation(value = "查询部门树")
    @GetMapping("/deptTree")
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }
}
